'use strict';

const base_RESTful = require('../core/base_RESTful.js');
const { config_cookies } = require('../config/config.js');
const jwt = require('jsonwebtoken');
const ms = require('ms');

exports.getDevice = async ctx => {
    const _this = new base_RESTful(ctx);
    const rq_data = ctx.request.body;
    const service = ctx.service;
    const query = ctx.query||{};
    const token = ctx.get("token");
    let result = null ;
  
    console.log( "getDevice",query)
    
    try{
      console.log("try_____________________")
      result = await service.device.findDevice(query);
    //  console.log({results,rowData})
    }catch(e){
      console.log("======findDevice Error=====")
      console.log(e);
    }
  

    console.log("======Device data=====")
    console.log(result)

    if (result) {
      ctx.body = {
        code: 2000,
        msg: '获取信息成功',
        data: result
      };
    } else {
      console.log("error")
      _this.error({
        code:5000,
        msg:"获取数据失败",
        data:[]
      });
    }
  };

  //  id: data.id,
  // deviceImg: data.img,
  exports.UspdateDevice = async ctx =>{
    const _this = new base_RESTful(ctx);
    const rq_data = ctx.request.body;
    const service = ctx.service;
    let result = null;
    try{
      console.log("try_____________________")
     result = await service.device.updateDevice(rq_data);
    }catch(e){
      console.log(e);
    }


    if (result) {
      ctx.body = {
        code: 2000,
        msg: '修改成功',
        data: result
      };
    } else {
      console.log("error")
      _this.error({
        code:5000,
        msg:"修改失败",
        data:[]
      });
    }
  }


  exports.CreateDevice = async ctx =>{
    const _this = new base_RESTful(ctx);
    const rq_data = ctx.request.body;
    const service = ctx.service;
    const token = ctx.get("token");
    let result = null;
    
    try{
      console.log("try_____________________");
      console.log(rq_data);
     result = await service.device.CreateDevice(rq_data);
     console.log("CreateDevice=====result")
    //  console.log(result)
    }catch(e){
      console.log(e);
    }

    if (result) {
      ctx.body = {
        code: 2000,
        msg: '添加成功',
        data: result
      };
    } else {
      console.log("error")
      _this.error({
        code:500,
        msg:"添加失败",
        data:[]
      });
    }

  }

  exports.DeleteDevice = async ctx =>{
    const _this = new base_RESTful(ctx);
    const rq_data = ctx.request.body;
    const service = ctx.service;
    const token = ctx.get("token");
    let result = null;

    if(rq_data.id === 100){
      _this.error({
        code:5000,
        msg:"这是root不能删除",
        data:[]
      });
      
    }

    try{
      console.log("try_____________________")
      console.log(rq_data)
     result = await service.device.deleteDevice(rq_data);
     console.log("result")
     console.log(result)
    }catch(e){
      console.log(e);
    }

    if (result) {
      ctx.body = {
        code: 2000,
        msg: '删除成功',
        data: result
      };
    } else {
      console.log("error")
      _this.error({
        code:5000,
        msg:"删除失败",
        data:[]
      });
    }

  }
